package com.power.services.department;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.department.DepartmentDAO;
import com.power.model.department.TiDepartment;
import com.power.model.hospital.TiHospital;
import com.power.util.Assent;
import com.power.util.PageUtil;

@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {

	@Autowired
	private DepartmentDAO departmentDAO;

	public void create(TiDepartment department) {
		this.departmentDAO.save(department);
	}

	public TiDepartment read(Integer id) {
		return ((TiDepartment) this.departmentDAO.load(id));
	}

	public void saveOrUpdate(TiDepartment department) {
		departmentDAO.saveOrUpdate(department);
	}

	public void delete(String ids) {
		String[] idAry = ids.split("_");
		for (int i = 0; i < idAry.length; i++) {
			Integer id = new Integer(idAry[i]);
			departmentDAO.deleteByKey(id);
		}
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> findAll(String departmentName, int start,
			int limit, Integer hid) {
		Map<String, Object> map = new HashMap<String, Object>();
		String str = " select  tm,ti   from TiDepartment  tm,TiHospital ti where  tm.hid = ti.id ";
		if (null != departmentName && !"".equals(departmentName)) {
			str += "  and  tm.name like '" + departmentName + "%'";
		}
		if (-1 != hid) {
			str += " and  tm.hid =" + hid;
		}
		str += " and tm.deleteFlag = 0";
		List<Object[]> data = departmentDAO.find(str, start, limit);
		List<Object[]> countData = departmentDAO.find(str);
		int count = countData == null ? 0 : countData.size();
		map.put("data", data);
		map.put("count", count);
		return map;
	}

	public String getProName(Integer code) {
		String sql = "SELECT a.name FROM t_s_district t ,t_s_district a WHERE t.upper_id = a.id AND t.id="
				+ code;
		List list = departmentDAO.findBySQL(sql);
		String proName = "";
		if (list.size() > 0)
			proName = list.get(0).toString();
		return proName;
	}

	public String getProId(Integer code) {
		String sql = "SELECT a.id FROM t_s_district t ,t_s_district a WHERE t.upper_id = a.id AND t.id="
				+ code;
		List list = departmentDAO.findBySQL(sql);
		String proName = "";
		if (list.size() > 0)
			proName = list.get(0).toString();
		return proName;
	}

	public String setDistrictName(Integer code) {
		String sql = "SELECT t.name FROM t_s_district t WHERE t.id = " + code;
		List list = departmentDAO.findBySQL(sql);
		String cityName = "";
		if (list.size() > 0)
			cityName = list.get(0).toString();
		return cityName;
	}

	public void delete(TiDepartment arg0) {
		departmentDAO.delete(arg0);
	}

	public void update(TiDepartment arg0) {
		departmentDAO.update(arg0);
	}

	public void deleteHos(String ids) {
		String[] idAry = ids.split("_");
		for (int i = 0; i < idAry.length; i++) {
			Integer id = new Integer(idAry[i]);
			departmentDAO
					.executeSql("update t_i_department set deleteFlag=1 where id="
							+ id);
		}
	}

	public Map<String, Object> findComboxAll(String name, int start, int limit,
			Integer hid) {
		List<TiDepartment> list = null;
		Map<String, Object> map = new HashMap<String, Object>();
		DetachedCriteria criteria = DetachedCriteria
				.forClass(TiDepartment.class);
		if (!Assent.checkNullAndEmpty(name))
			criteria.add(Restrictions.like("name", "%" + name + "%"));
		criteria.add(Restrictions.eq("deleteFlag", "0"));
		if (-1 != hid)
			criteria.add(Restrictions.eq("hid", hid));
		list = departmentDAO.findByCriteria(criteria, start, limit);
		int count = departmentDAO.getRowCount(criteria);
		if (null == list)
			list = new ArrayList<TiDepartment>();
		map.put("data", list);
		map.put("count", count);
		return map;
	}

	@SuppressWarnings("unchecked")
	public List<TiDepartment> findByHospitalId(Integer hid) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TiDepartment.class);
		criteria.add(Restrictions.eq("hid", hid));
		return departmentDAO.findByCriteria(criteria);
	}

	public PageUtil searchDept(String keyword, int pageNo, int limit) {
		PageUtil pu = new PageUtil();
		DetachedCriteria criteria = DetachedCriteria.forClass(TiDepartment.class);
		criteria.add(Restrictions.like("name", "%" + keyword + "%"));
		criteria.addOrder(Order.asc("firstChar"));
		List<TiDepartment> list = departmentDAO.findByCriteria(criteria, (pageNo-1)*limit, limit);
		pu.setList(list);
		pu.setPageNo(pageNo);
		pu.setPageSize(limit);
		pu.setTotalRecords(departmentDAO.getRowCount(criteria));
		return pu;
	}

	public List<TiDepartment> findBySearchKeyWord(String keyword) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TiDepartment.class);
		criteria.add(Restrictions.like("name", "%" + keyword + "%"));
		criteria.addOrder(Order.asc("firstChar"));
		List<TiDepartment> list = departmentDAO.findByCriteria(criteria, 0, 10);
		return list;
	}
}